﻿//07.Write a program that encodes and decodes a string using given encryption key (cipher). The key consists of a sequence of characters. The encoding/decoding is done by performing XOR (exclusive or) operation over the first letter of the string with the first of the key, the second – with the second, etc. When the last key character is reached, the next is the first.

using System;
using System.Text;

namespace Encryption
{
    class Encryption
    {
        static void Main()
        {
            string text = "Study hard!";
            string cipher = "You will succeed!";

            string encoded = Encrypt(text, cipher);
            string decoded = Encrypt(encoded, cipher);

            Console.WriteLine("Original text: {0}", text);
            Console.WriteLine();
            Console.WriteLine("Encoded message: {0}", encoded);
            Console.WriteLine();
            Console.WriteLine("Decoded message: {0}", decoded);
        }

        private static string Encrypt(string text, string cipher)
        {
            StringBuilder message = new StringBuilder();

            for (int i = 0; i < text.Length; i += cipher.Length)
            {
                for (int j = 0; j < cipher.Length; j++)
                {
                    if (i + j > text.Length - 1)
                    {
                        break;
                    }
                    char temp = (char)(text[i + j] ^ cipher[j]);
                    message.Append(temp);
                }
            }
            return message.ToString();
        }
    }
}
